package xyz.haijin.shiro.service.impl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import xyz.haijin.shiro.dao.RoleDAO;
import xyz.haijin.shiro.entity.Role;
import xyz.haijin.shiro.filter.MyShiroFilterFactoryBean;
import xyz.haijin.shiro.service.RoleService;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class RoleServiceImpl implements RoleService {

    @Resource
    private RoleDAO roleDAO;

    @Override
    public List<Role> getRoleList() {
        return roleDAO.getRoleList();
    }

    @Override
    public void addRole(Role role) {
        roleDAO.addRole(role);
    }

    @Override
    public void updateRole(Role role) {
        roleDAO.updateRole(role);
    }

    @Autowired
    private MyShiroFilterFactoryBean shiroFilterFactoryBean;

    @Override
    public void addRoleMenu(Integer roleId, Integer[] menuIds) {
        roleDAO.deleteRoleMenu(roleId);
        Map<String, Integer> param = new HashMap<>();
        param.put("roleId", roleId);
        for (Integer menuId : menuIds) {
            param.put("menuId", menuId);
            roleDAO.addRoleMenu(param);
        }
        shiroFilterFactoryBean.update();
    }

    @Override
    public List<Integer> getRoleMenuIds(Integer roleId) {
        return roleDAO.getRoleMenuIds(roleId);
    }
}
